#ifndef BASEPROTO_H
#define BASEPROTO_H

#include "proto-lib_global.h"
#include <google/protobuf/message.h>
#include <qmetatype.h>
#include <QObject>

#define ENLARGE_STEP 2
#define BEGIN_SIZE 2048

class ProtoType : public QObject{
    Q_OBJECT
public:
    enum MessageType
    {
        HEARTBEAT_REQUEST = 1,
        LOGIN_REQUEST,
        LOGIN_RESPONSE,
        FILE_PATHS_NOTICE
    };
    Q_ENUM(MessageType)
};

Q_DECLARE_METATYPE(ProtoType*);

class PROTOLIB_EXPORT BaseProto
{
private:
    char *_buf;
    uint32_t _size;
    char *_receiveBuf;
    uint32_t _receiveSize;
    uint32_t _curReceiveMsgSize;
    char *_receiveBufTail;
public:
    BaseProto();
    inline char *getBuf()
    {
        return _buf;
    }
    inline uint32_t getCurReceiveMsgSize()
    {
        return _curReceiveMsgSize;
    }
    inline char *getReceiveBuf()
    {
        return _receiveBuf;
    }
    char* addToReceiveBuf(uint32_t &handleSize, char *buf, uint32_t size);
    inline uint32_t getSize()
    {
        return _size;
    }
    char *getBuf(uint32_t size);
    char *getReceiveBuf(uint32_t size);
    //if return null, the package is larger than MAX_PACKAGE
    char *encode(unsigned int type, google::protobuf::Message& msg, uint32_t &size);
    bool decode(const char *msg, google::protobuf::Message &m);
    bool decode(const char *msg, int size, google::protobuf::Message &m);
};

#endif // BASEPROTO_H
